package com.nchu.student_score.dataObject;

import org.springframework.util.DigestUtils;
import java.text.SimpleDateFormat;
import java.util.*;

public class PasswordCheck{
    private static final int key = 12345;
    private static String time = null;
    private static final SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd");

    private PasswordCheck(){
        isoFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    }

    public static String getUserKey(String sqlPwd){
        time = isoFormat.format(new Date());
        String result = sqlPwd + time + key;
        return DigestUtils.md5DigestAsHex(result.getBytes());
    }

    public static boolean checkUserKey(String userKey,String sqlPwd){
        time = isoFormat.format(new Date());
        String sqlKey = DigestUtils.md5DigestAsHex((sqlPwd + time + key).getBytes());
        return userKey.equals(sqlKey);
    }
}
